
const vscode = require('vscode');
const { clearInterval } = require('timers');

// 编辑器节点
let bookBar = null
// 存储解析出来的文档内容
let bookContent
// 当前页数
let pageIndex = 1
// 每页多少字
let pageSize = 30
// 总页数
let total = 0
// 自动播放定时器
let timer = null
// 是否使用boss模式
let isBoss = false
// 定时器间隔时间
let autoInterval = 3000
// 是否自动播放参数
let auto = false

/** 监听配置参数变化，执行相应的更新 */
const changeConfig = () => {
    const newPageIndex = ~~vscode.workspace.getConfiguration().get('vscodeMiniBook.pageIndex')
    const newPageSize = ~~vscode.workspace.getConfiguration().get('vscodeMiniBook.pageSize')
    const newAuto =  ~~vscode.workspace.getConfiguration().get('vscodeMiniBook.auto')
    const newAutoInterval =  ~~vscode.workspace.getConfiguration().get('vscodeMiniBook.autoInterval')

    /** 如果新的自动播放不等于老的，而且是 false, 同时有定时器在跑则关闭自动播放 */
    if (newAuto !== auto && !newAuto && timer) {
        autoRead('stop')
    }

    /** 当每页字数改变，当前页没变，则重新计算当前页数 */
    if (newPageSize !== pageSize && newPageIndex === pageIndex) {
        const offsetPageIndex = ~~(pageIndex * pageSize / newPageSize)
        currentContent()
        vscode.workspace.getConfiguration().update('vscodeMiniBook.pageIndex', offsetPageIndex.toString(), true);
    }
    
    /**当前页和每页字数都改变则重新计算内容 */
    if (newPageSize !== pageSize && newPageIndex !== pageIndex) {
        pageIndex = newPageIndex
        pageSize = newPageSize
        currentContent()
    }

    /** 当当前页数发生变化，每页字数不变则更新当前页数 */
    if (newPageSize === pageSize && newPageIndex !== pageIndex) {
        pageIndex = newPageIndex
        currentContent()
    }

    /** 如果定时间隔参数发生变化则重新跑自动播放 */
    if (newAutoInterval !== autoInterval && timer) {
        autoInterval = newAutoInterval
        autoRead('stop')
        autoRead('start')
    }

    return true
}

/**
 * 初始加载参数
 */
const loadConfig = () => {
    pageIndex = ~~vscode.workspace.getConfiguration().get('vscodeMiniBook.pageIndex')
    pageSize =  ~~vscode.workspace.getConfiguration().get('vscodeMiniBook.pageSize')
    autoInterval = ~~vscode.workspace.getConfiguration().get('vscodeMiniBook.autoInterval')
    auto = vscode.workspace.getConfiguration().get('vscodeMiniBook.auto')
}

/** 检查是否有菜单元素 */
const createBookBar = () => {
    !bookBar && (bookBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.left, 999))
}

/** 后退 */
const back = () => {
    auto ? null : autoRead('stop')
    if (pageIndex < 2) return false
    const value = bookContent.substr((pageIndex - 2) * pageSize, pageSize)
    pageIndex--
    vscode.workspace.getConfiguration().update('vscodeMiniBook.pageIndex', pageIndex.toString(), true);
    setShowContent(value)
}

/** 前进 */
const goTo = () => {
    vscode.workspace.getConfiguration().get('vscodeMiniBook.auto') ? null : autoRead('stop')
    if (pageIndex > total) return false
    const value = bookContent.substr(pageIndex++ * pageSize, pageSize)
    // 更新当前页配置
    vscode.workspace.getConfiguration().update('vscodeMiniBook.pageIndex', pageIndex.toString(), true);
    setShowContent(value)
}

const currentContent = () => {
    const value = bookContent.substr((pageIndex - 1) * pageSize, pageSize)
    setShowContent(value)
}

/** 自动切换 */
const autoRead = (type) => {
    if ('start' === type) {
        !timer && (timer = setInterval(goTo, autoInterval))
    } else {
        clearInterval(timer)
        timer = null
    }
}

/** 老板键 */
const bossKey = () => {
    if (isBoss) {
        currentContent()
        isBoss = false
    } else {
        bookBar.text = 'Document'
        isBoss = true
        bookBar.show()
    }
}

/** 设置展示内容 */
const setShowContent = (value) => {
    createBookBar()
    bookBar.text = `${value}       ${pageIndex} / ${total}`
    bookBar.show()
}

/** 本地存储加载之后的文档内容 */
const setBookContent = (content) => {
    createBookBar()
    bookContent = content
    total = ~~(bookContent.length / pageSize) || 1
    currentContent()
}

module.exports = {
    back,
    goTo,
    setBookContent,
    bossKey,
    loadConfig,
    autoRead,
    changeConfig
}